Array.prototype.inArray = function(str){
    var len = this.length;
    for(var i = 0;i<len;i++){
        if(this[i] === str){
            return true;
        }
    }
    return false;
}

Array.prototype.multiSort = function(key,order){
    return $.sort(this,key,order);
}


global.$={}
//Multiple sort
$.sort=function(arr,key,order){
    if (arr.length == 0){
        return [];
    }
    if(!key){
        return arr;
    }
    var orderKeys={};
    if(typeof key == 'object'){
        orderKeys = key;
    }
    else{
        orderKeys[key] = order;
    }

    var lRows = new Array();
    var rRows = new Array();
    var arrBase = arr[0];
    var sortCompare = function(obj){
        for(var k in orderKeys){
            var v = orderKeys[k];
            var pv = arrBase[k]||0;
            var tv = obj[k]||0;
            if(pv > tv){
                return v=='asc'?-1:1;
            }
            if(pv < tv){
                return v=='asc'?1:-1;
            }
        }
        return 0;
    }

    var length = arr.length;
    for (var i = 1; i < length; i++) {
        var flag = sortCompare(arr[i]);
        flag >= 0 ? rRows.push(arr[i]) : lRows.push(arr[i]);
    }
    return $.sort(lRows,orderKeys).concat(arrBase, $.sort(rRows,orderKeys));
}

//merge object
$.merge=function (obj1, obj2) {
    if (typeof obj1 !== 'object' || typeof obj2 !== 'object') {
        return obj2;
    }
    for (var k in obj2) {
        var v = obj2[k];
        if (typeof obj1[k] === 'object' && typeof v === 'object') {
            obj1[k] = $.merge(obj1[k], v);
        }
        else {
            obj1[k] = v;
        }
    }
    return obj1;
}

$.dataFormat=function (val, type) {
    if (type == 'int') {
        return parseInt(val, 10);
    }
    else if (type == 'number' || type == 'float') {
        return parseFloat(val);
    }
    else if (type == 'json' || type == 'object') {
        return JSON.parse(val);
    }
    else if (type == 'string') {
        return val ? val.toString() : '';
    }
    else {
        return val;
    }
}

$.dataSelect=function($data,$keys){
     if(!$data){
         return false;
     }
    var dsGetVal = function($d,$k){
        return $d[$k] || null;
    }
    var dsGetObj = function($k){
        var arr = $k.toString().split('.');
        var len = arr.length;
        var val = $data;
        for(var i=0;i<len;i++){
            var $k = arr[i];
            var $v = dg(val,$k);
            if($v===null){
                return $v;
            }
            val = $v;
        }
        return val;
    }

    var dsGetOne = function($k){
        var tag = $k.toString().indexOf('.');
        if(tag >=0){
            return dsGetObj($k);
        }
        else{
            return dsGetVal($data,$k);
        }
    }
    if(!$keys){
        return $data;
    }
    else if(Array.isArray($keys)){
        var $vals = {};
        for(var $i in $keys){
            var $k = $keys[$i];
            $vals[$k] = dsGetOne($k);
        }
        return $vals;
    }
    else{
        return dsGetOne($keys);
    }

}
//loader
$.loader = function (name) {
    var config = require('../config');
    var $index = name.indexOf('/');
    var $model = name.substring(0, $index);
    var $dir = config['share'][$model] || config['root'];
    var $path = $dir+'/'+name;
    return require($path);
}

$.callback = function(err,ret){
    if(err){
        return false;
    }
    else{
        return ret;
    }
}
//config,format,callback
$.redis = function(config,callback){
    var redis = require('./redis');
    if(arguments.length == 3){
        var type = arguments[1];
        var func = arguments[2] || $.callback;
        if(redis[type]){
            func(null,redis[type]( arguments[0]) );
        }
        else{
            redis.conn(config,callback);
        }
    }
    else{
        redis.conn(config,callback);
    }
}

$.mongo = function(config,callback){
    var mongo = require('./mongo');
    if(arguments.length == 4){
        mongo.coll(arguments[0],arguments[1],arguments[2],arguments[3]);
    }
    else{
        mongo.conn(config,callback);
    }
}

$.md5 = function(str){
    var crypto = require('crypto');
    var _encrymd5 = crypto.createHash('md5');
    _encrymd5.update(str);
    return _encrymd5.digest('hex');
}

$.base64 = {
    encode:function(str) {
        var code = new Buffer(str);
        return code.toString('base64');
    },
    decode:function (str) {
        var code = new Buffer(str, 'base64')
        return code.toString();
    }
}